package labox.innovation.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javolution.util.FastList;
import javolution.util.FastMap;
import labox.innovation.gameserver.util.StringUtil;
import labox.innovation.util.L2Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class L2jmodsConfig {
    private static final Logger _log = LoggerFactory.getLogger(L2jmodsConfig.class.getName());

    private static final String L2JMOD_CONFIG_FILE = "./config/l2jmods.properties";
    // --------------------------------------------------
    // L2JMods Settings
    // --------------------------------------------------

    public static boolean TVT_EVENT_ENABLED;

    public static boolean TVT_EVENT_IN_INSTANCE;

    public static String TVT_EVENT_INSTANCE_FILE;

    public static String[] TVT_EVENT_INTERVAL;

    public static int TVT_EVENT_PARTICIPATION_TIME;

    public static int TVT_EVENT_RUNNING_TIME;

    public static int TVT_EVENT_PARTICIPATION_NPC_ID;

    public static int[] TVT_EVENT_PARTICIPATION_NPC_COORDINATES = new int[3];

    public static int[] TVT_EVENT_PARTICIPATION_FEE = new int[2];

    public static int TVT_EVENT_MIN_PLAYERS_IN_TEAMS;

    public static int TVT_EVENT_MAX_PLAYERS_IN_TEAMS;

    public static int TVT_EVENT_RESPAWN_TELEPORT_DELAY;

    public static int TVT_EVENT_START_LEAVE_TELEPORT_DELAY;

    public static String TVT_EVENT_TEAM_1_NAME;

    public static int[] TVT_EVENT_TEAM_1_COORDINATES = new int[3];

    public static String TVT_EVENT_TEAM_2_NAME;

    public static int[] TVT_EVENT_TEAM_2_COORDINATES = new int[3];

    public static List<int[]> TVT_EVENT_REWARDS = new FastList<int[]>();

    public static boolean TVT_EVENT_TARGET_TEAM_MEMBERS_ALLOWED;

    public static boolean TVT_EVENT_SCROLL_ALLOWED;

    public static boolean TVT_EVENT_POTIONS_ALLOWED;

    public static boolean TVT_EVENT_SUMMON_BY_ITEM_ALLOWED;

    public static List<Integer> TVT_DOORS_IDS_TO_OPEN = new ArrayList<Integer>();

    public static List<Integer> TVT_DOORS_IDS_TO_CLOSE = new ArrayList<Integer>();

    public static boolean TVT_REWARD_TEAM_TIE;

    public static byte TVT_EVENT_MIN_LVL;

    public static byte TVT_EVENT_MAX_LVL;

    public static int TVT_EVENT_EFFECTS_REMOVAL;

    public static Map<Integer, Integer> TVT_EVENT_FIGHTER_BUFFS;

    public static Map<Integer, Integer> TVT_EVENT_MAGE_BUFFS;

    public static boolean TVT_ALLOW_VOICED_COMMAND;

    public static boolean L2JMOD_ALLOW_WEDDING;

    public static int L2JMOD_WEDDING_PRICE;

    public static boolean L2JMOD_WEDDING_PUNISH_INFIDELITY;

    public static boolean L2JMOD_WEDDING_TELEPORT;

    public static int L2JMOD_WEDDING_TELEPORT_PRICE;

    public static int L2JMOD_WEDDING_TELEPORT_DURATION;

    public static boolean L2JMOD_WEDDING_SAMESEX;

    public static boolean L2JMOD_WEDDING_FORMALWEAR;

    public static int L2JMOD_WEDDING_DIVORCE_COSTS;

    public static boolean BANKING_SYSTEM_ENABLED;

    public static int BANKING_SYSTEM_GOLDBARS;

    public static int BANKING_SYSTEM_ADENA;

    public static boolean L2JMOD_ENABLE_WAREHOUSESORTING_CLAN;

    public static boolean L2JMOD_ENABLE_WAREHOUSESORTING_PRIVATE;

    public static boolean L2JMOD_ENABLE_WAREHOUSESORTING_FREIGHT;

    public static boolean OFFLINE_CRAFT_ENABLE;

    public static boolean OFFLINE_SET_NAME_COLOR;

    public static int OFFLINE_NAME_COLOR;

    public static boolean L2JMOD_ENABLE_MANA_POTIONS_SUPPORT;

    public static void load() {
        _log.info("Loading netcore Configuration Files...", Config.SERVER_ID);
        InputStream is = null;
        try {
            L2Properties L2JModSettings = new L2Properties();
            is = new FileInputStream(new File(L2JMOD_CONFIG_FILE));
            L2JModSettings.load(is);

            TVT_EVENT_ENABLED = Boolean.parseBoolean(L2JModSettings.getProperty("TvTEventEnabled", "false"));
            TVT_EVENT_IN_INSTANCE = Boolean.parseBoolean(L2JModSettings.getProperty("TvTEventInInstance", "false"));
            TVT_EVENT_INSTANCE_FILE = L2JModSettings.getProperty("TvTEventInstanceFile", "coliseum.xml");
            TVT_EVENT_INTERVAL = L2JModSettings.getProperty("TvTEventInterval", "20:00").split(",");
            TVT_EVENT_PARTICIPATION_TIME = Integer.parseInt(L2JModSettings.getProperty("TvTEventParticipationTime", "3600"));
            TVT_EVENT_RUNNING_TIME = Integer.parseInt(L2JModSettings.getProperty("TvTEventRunningTime", "1800"));
            TVT_EVENT_PARTICIPATION_NPC_ID = Integer.parseInt(L2JModSettings.getProperty("TvTEventParticipationNpcId", "0"));

            L2JMOD_ALLOW_WEDDING = Boolean.parseBoolean(L2JModSettings.getProperty("AllowWedding", "False"));
            L2JMOD_WEDDING_PRICE = Integer.parseInt(L2JModSettings.getProperty("WeddingPrice", "250000000"));
            L2JMOD_WEDDING_PUNISH_INFIDELITY = Boolean.parseBoolean(L2JModSettings.getProperty("WeddingPunishInfidelity", "True"));
            L2JMOD_WEDDING_TELEPORT = Boolean.parseBoolean(L2JModSettings.getProperty("WeddingTeleport", "True"));
            L2JMOD_WEDDING_TELEPORT_PRICE = Integer.parseInt(L2JModSettings.getProperty("WeddingTeleportPrice", "50000"));
            L2JMOD_WEDDING_TELEPORT_DURATION = Integer.parseInt(L2JModSettings.getProperty("WeddingTeleportDuration", "60"));
            L2JMOD_WEDDING_SAMESEX = Boolean.parseBoolean(L2JModSettings.getProperty("WeddingAllowSameSex", "False"));
            L2JMOD_WEDDING_FORMALWEAR = Boolean.parseBoolean(L2JModSettings.getProperty("WeddingFormalWear", "True"));
            L2JMOD_WEDDING_DIVORCE_COSTS = Integer.parseInt(L2JModSettings.getProperty("WeddingDivorceCosts", "20"));

            L2JMOD_ENABLE_WAREHOUSESORTING_CLAN = Boolean.valueOf(L2JModSettings.getProperty("EnableWarehouseSortingClan", "False"));
            L2JMOD_ENABLE_WAREHOUSESORTING_PRIVATE = Boolean.valueOf(L2JModSettings.getProperty("EnableWarehouseSortingPrivate", "False"));
            L2JMOD_ENABLE_WAREHOUSESORTING_FREIGHT = Boolean.valueOf(L2JModSettings.getProperty("EnableWarehouseSortingFreight", "False"));

            if (TVT_EVENT_PARTICIPATION_NPC_ID == 0) {
                TVT_EVENT_ENABLED = false;
                _log.warn("TvTEventEngine[Config.load()]: invalid config property -> TvTEventParticipationNpcId");
            } else {
                String[] propertySplit = L2JModSettings.getProperty("TvTEventParticipationNpcCoordinates", "0,0,0").split(",");
                if (propertySplit.length < 3) {
                    TVT_EVENT_ENABLED = false;
                    _log.warn("TvTEventEngine[Config.load()]: invalid config property -> TvTEventParticipationNpcCoordinates");
                } else {
                    TVT_EVENT_REWARDS = new FastList<int[]>();
                    TVT_DOORS_IDS_TO_OPEN = new ArrayList<Integer>();
                    TVT_DOORS_IDS_TO_CLOSE = new ArrayList<Integer>();
                    TVT_EVENT_PARTICIPATION_NPC_COORDINATES = new int[3];
                    TVT_EVENT_TEAM_1_COORDINATES = new int[3];
                    TVT_EVENT_TEAM_2_COORDINATES = new int[3];
                    TVT_EVENT_PARTICIPATION_NPC_COORDINATES[0] = Integer.parseInt(propertySplit[0]);
                    TVT_EVENT_PARTICIPATION_NPC_COORDINATES[1] = Integer.parseInt(propertySplit[1]);
                    TVT_EVENT_PARTICIPATION_NPC_COORDINATES[2] = Integer.parseInt(propertySplit[2]);
                    TVT_EVENT_MIN_PLAYERS_IN_TEAMS = Integer.parseInt(L2JModSettings.getProperty("TvTEventMinPlayersInTeams", "1"));
                    TVT_EVENT_MAX_PLAYERS_IN_TEAMS = Integer.parseInt(L2JModSettings.getProperty("TvTEventMaxPlayersInTeams", "20"));
                    TVT_EVENT_MIN_LVL = (byte) Integer.parseInt(L2JModSettings.getProperty("TvTEventMinPlayerLevel", "1"));
                    TVT_EVENT_MAX_LVL = (byte) Integer.parseInt(L2JModSettings.getProperty("TvTEventMaxPlayerLevel", "80"));
                    TVT_EVENT_RESPAWN_TELEPORT_DELAY = Integer.parseInt(L2JModSettings.getProperty("TvTEventRespawnTeleportDelay", "20"));
                    TVT_EVENT_START_LEAVE_TELEPORT_DELAY = Integer.parseInt(L2JModSettings.getProperty("TvTEventStartLeaveTeleportDelay", "20"));
                    TVT_EVENT_EFFECTS_REMOVAL = Integer.parseInt(L2JModSettings.getProperty("TvTEventEffectsRemoval", "0"));
                    TVT_ALLOW_VOICED_COMMAND = Boolean.parseBoolean(L2JModSettings.getProperty("TvTAllowVoicedInfoCommand", "false"));
                    TVT_EVENT_TEAM_1_NAME = L2JModSettings.getProperty("TvTEventTeam1Name", "Team1");
                    propertySplit = L2JModSettings.getProperty("TvTEventTeam1Coordinates", "0,0,0").split(",");
                    if (propertySplit.length < 3) {
                        TVT_EVENT_ENABLED = false;
                        _log.warn("TvTEventEngine[Config.load()]: invalid config property -> TvTEventTeam1Coordinates");
                    } else {
                        TVT_EVENT_TEAM_1_COORDINATES[0] = Integer.parseInt(propertySplit[0]);
                        TVT_EVENT_TEAM_1_COORDINATES[1] = Integer.parseInt(propertySplit[1]);
                        TVT_EVENT_TEAM_1_COORDINATES[2] = Integer.parseInt(propertySplit[2]);
                        TVT_EVENT_TEAM_2_NAME = L2JModSettings.getProperty("TvTEventTeam2Name", "Team2");
                        propertySplit = L2JModSettings.getProperty("TvTEventTeam2Coordinates", "0,0,0").split(",");
                        if (propertySplit.length < 3) {
                            TVT_EVENT_ENABLED = false;
                            _log.warn("TvTEventEngine[Config.load()]: invalid config property -> TvTEventTeam2Coordinates");
                        } else {
                            TVT_EVENT_TEAM_2_COORDINATES[0] = Integer.parseInt(propertySplit[0]);
                            TVT_EVENT_TEAM_2_COORDINATES[1] = Integer.parseInt(propertySplit[1]);
                            TVT_EVENT_TEAM_2_COORDINATES[2] = Integer.parseInt(propertySplit[2]);
                            propertySplit = L2JModSettings.getProperty("TvTEventParticipationFee", "0,0").split(",");
                            try {
                                TVT_EVENT_PARTICIPATION_FEE[0] = Integer.parseInt(propertySplit[0]);
                                TVT_EVENT_PARTICIPATION_FEE[1] = Integer.parseInt(propertySplit[1]);
                            } catch (NumberFormatException nfe) {
                                if (propertySplit.length > 0)
                                    _log.warn("TvTEventEngine[Config.load()]: invalid config property -> TvTEventParticipationFee");
                            }
                            propertySplit = L2JModSettings.getProperty("TvTEventReward", "57,100000").split(";");
                            for (String reward : propertySplit) {
                                String[] rewardSplit = reward.split(",");
                                if (rewardSplit.length != 2)
                                    _log.warn(StringUtil.concat("TvTEventEngine[Config.load()]: invalid config property -> TvTEventReward \"", reward, "\""));
                                else {
                                    try {
                                        TVT_EVENT_REWARDS.add(new int[] { Integer.parseInt(rewardSplit[0]), Integer.parseInt(rewardSplit[1]) });
                                    } catch (NumberFormatException nfe) {
                                        if (!reward.isEmpty())
                                            _log.warn(StringUtil.concat("TvTEventEngine[Config.load()]: invalid config property -> TvTEventReward \"", reward, "\""));
                                    }
                                }
                            }

                            TVT_EVENT_TARGET_TEAM_MEMBERS_ALLOWED = Boolean.parseBoolean(L2JModSettings.getProperty("TvTEventTargetTeamMembersAllowed", "true"));
                            TVT_EVENT_SCROLL_ALLOWED = Boolean.parseBoolean(L2JModSettings.getProperty("TvTEventScrollsAllowed", "false"));
                            TVT_EVENT_POTIONS_ALLOWED = Boolean.parseBoolean(L2JModSettings.getProperty("TvTEventPotionsAllowed", "false"));
                            TVT_EVENT_SUMMON_BY_ITEM_ALLOWED = Boolean.parseBoolean(L2JModSettings.getProperty("TvTEventSummonByItemAllowed", "false"));
                            TVT_REWARD_TEAM_TIE = Boolean.parseBoolean(L2JModSettings.getProperty("TvTRewardTeamTie", "false"));
                            propertySplit = L2JModSettings.getProperty("TvTDoorsToOpen", "").split(";");
                            for (String door : propertySplit) {
                                try {
                                    TVT_DOORS_IDS_TO_OPEN.add(Integer.parseInt(door));
                                } catch (NumberFormatException nfe) {
                                    if (!door.equals(""))
                                        _log.warn(StringUtil.concat("TvTEventEngine[Config.load()]: invalid config property -> TvTDoorsToOpen \"", door, "\""));
                                }
                            }

                            propertySplit = L2JModSettings.getProperty("TvTDoorsToClose", "").split(";");
                            for (String door : propertySplit) {
                                try {
                                    TVT_DOORS_IDS_TO_CLOSE.add(Integer.parseInt(door));
                                } catch (NumberFormatException nfe) {
                                    if (!door.isEmpty())
                                        _log.warn(StringUtil.concat("TvTEventEngine[Config.load()]: invalid config property -> TvTDoorsToClose \"", door, "\""));
                                }
                            }

                            propertySplit = L2JModSettings.getProperty("TvTEventFighterBuffs", "").split(";");
                            if (!propertySplit[0].equals("")) {
                                TVT_EVENT_FIGHTER_BUFFS = new FastMap<Integer, Integer>(propertySplit.length);
                                for (String skill : propertySplit) {
                                    String[] skillSplit = skill.split(",");
                                    if (skillSplit.length != 2)
                                        _log.warn(StringUtil.concat("TvTEventEngine[Config.load()]: invalid config property -> TvTEventFighterBuffs \"", skill, "\""));
                                    else {
                                        try {
                                            TVT_EVENT_FIGHTER_BUFFS.put(Integer.parseInt(skillSplit[0]), Integer.parseInt(skillSplit[1]));
                                        } catch (NumberFormatException nfe) {
                                            if (!skill.isEmpty())
                                                _log.warn(StringUtil.concat("TvTEventEngine[Config.load()]: invalid config property -> TvTEventFighterBuffs \"", skill, "\""));
                                        }
                                    }
                                }
                            }

                            propertySplit = L2JModSettings.getProperty("TvTEventMageBuffs", "").split(";");
                            if (!propertySplit[0].equals("")) {
                                TVT_EVENT_MAGE_BUFFS = new FastMap<Integer, Integer>(propertySplit.length);
                                for (String skill : propertySplit) {
                                    String[] skillSplit = skill.split(",");
                                    if (skillSplit.length != 2)
                                        _log.warn(StringUtil.concat("TvTEventEngine[Config.load()]: invalid config property -> TvTEventMageBuffs \"", skill, "\""));
                                    else {
                                        try {
                                            TVT_EVENT_MAGE_BUFFS.put(Integer.parseInt(skillSplit[0]), Integer.parseInt(skillSplit[1]));
                                        } catch (NumberFormatException nfe) {
                                            if (!skill.isEmpty())
                                                _log.warn(StringUtil.concat("TvTEventEngine[Config.load()]: invalid config property -> TvTEventMageBuffs \"", skill, "\""));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            BANKING_SYSTEM_ENABLED = Boolean.parseBoolean(L2JModSettings.getProperty("BankingEnabled", "false"));
            BANKING_SYSTEM_GOLDBARS = Integer.parseInt(L2JModSettings.getProperty("BankingGoldbarCount", "1"));
            BANKING_SYSTEM_ADENA = Integer.parseInt(L2JModSettings.getProperty("BankingAdenaCount", "500000000"));

            OFFLINE_CRAFT_ENABLE = Boolean.parseBoolean(L2JModSettings.getProperty("OfflineCraftEnable", "false"));
            OFFLINE_SET_NAME_COLOR = Boolean.parseBoolean(L2JModSettings.getProperty("OfflineSetNameColor", "false"));
            OFFLINE_NAME_COLOR = Integer.decode("0x" + L2JModSettings.getProperty("OfflineNameColor", "808080"));

            L2JMOD_ENABLE_MANA_POTIONS_SUPPORT = Boolean.parseBoolean(L2JModSettings.getProperty("EnableManaPotionSupport", "false"));

        } catch (Exception e) {
            _log.error("Failed to Load " + L2JMOD_CONFIG_FILE + " File.", e);
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                _log.error("InputStream close occured IOException");
            }
        }
    }
}
